package cn.iocoder.yudao.module.diy.controller.admin.poster;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.diy.*;
import cn.iocoder.yudao.module.diy.dal.diy.PosterTypeEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.successAdmin;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.diy.controller.admin.poster.vo.*;
import cn.iocoder.yudao.module.diy.dal.dataobject.poster.PosterDO;
import cn.iocoder.yudao.module.diy.service.poster.PosterService;

@Tag(name = "管理后台 - 装修海报")
@RestController
@RequestMapping("/diy/poster")
@Validated
public class PosterController {

    @Resource
    private PosterService posterService;


    @PostMapping("")
    @Operation(summary = "创建海报")
    //@PreAuthorize("@ss.hasPermission('system:poster:create')")
    public CommonResult<Long> createPoster(@Valid @RequestBody PosterSaveReqVO createReqVO) {
        return successAdmin(posterService.createPoster(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新海报")
    //@PreAuthorize("@ss.hasPermission('system:poster:update')")
    public CommonResult<Boolean> updatePoster(@Valid @RequestBody PosterSaveReqVO updateReqVO) {
        posterService.updatePoster(updateReqVO);
        return successAdmin(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除海报")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('system:poster:delete')")
    public CommonResult<Boolean> deletePoster(@RequestParam("id") Long id) {
        posterService.deletePoster(id);
        return successAdmin(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得海报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('system:poster:query')")
    public CommonResult<PosterRespVO> getPoster(@RequestParam("id") Long id) {
        PosterDO poster = posterService.getPoster(id);
        return successAdmin(BeanUtils.toBean(poster, PosterRespVO.class));
    }

    @GetMapping("")
    @Operation(summary = "获得海报分页")
    //@PreAuthorize("@ss.hasPermission('system:poster:query')")
    public CommonResult<PageResult<PosterRespVO>> getPosterPage(@Valid PosterPageReqVO pageReqVO) {
        PageResult<PosterDO> pageResult = posterService.getPosterPage(pageReqVO);
        return successAdmin(BeanUtils.toBean(pageResult, PosterRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出海报 Excel")
    //@PreAuthorize("@ss.hasPermission('system:poster:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPosterExcel(@Valid PosterPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PosterDO> list = posterService.getPosterPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "海报.xls", "数据", PosterRespVO.class, BeanUtils.toBean(list, PosterRespVO.class));
    }

    /**
     * 海报类型
     *
     * @param type
     * @return
     */
    @GetMapping("/type")
    public CommonResult<Object> type(@RequestParam(name = "type", defaultValue = "") String type) {
        if (type.isEmpty()) {
            return successAdmin(PosterTypeEnum.getType());
        }
        return successAdmin(PosterTypeEnum.getType(type));
    }

    /**
     * 自定义海报初始化数据
     *
     * @return
     */
    @GetMapping("/init")
    public CommonResult init(PosterInitParam param) {
        return successAdmin(posterService.init(param));
    }

    /**
     * 海报模板
     *
     * @return
     */
    @GetMapping("/template")
    public CommonResult template(PosterTemplateSearchParam param) {
        return successAdmin(posterService.template(param.getAddon(), param.getType()));
    }

    /**
     * 海报编辑
     * @param editParam 编辑参数
     * @return Result<Object>
     */
    @PutMapping("/{id}")
    public CommonResult<Object> edit(@PathVariable("id") Long id, @Validated @RequestBody SysPosterParam editParam) {
        posterService.edit(id, editParam);
        return successAdmin(true);
    }




    /**
     * 修改自定义海报状态
     *
     * @return
     */
    @PutMapping("/status")
    public CommonResult modifyStatus(@RequestBody PosterModifyParam param) {
        posterService.modifyStatus(param.getId(), param.getStatus());
        return successAdmin("操作成功！");
    }

    /**
     * 将自定义海报修改为默认海报
     *
     * @return
     */
    @PutMapping("/default")
    public CommonResult modifyDefault(@RequestBody PosterModifyParam param) {
        posterService.modifyDefault(param.getId());
        return successAdmin("操作成功！");
    }

    /**
     * 生成海报
     * @param param
     * @return
     */
    @GetMapping("/generate")
    public CommonResult<?> poster(GetPosterParam param) {
        param.setSiteId(TenantContextHolder.getTenantId());
        param.setChannel(ObjectUtil.defaultIfNull(param.getChannel(), "h5"));
        return successAdmin( posterService.get(param));
    }

    /**
     * 预览海报
     * @param param
     * @return
     */
    @GetMapping("/preview")
    public CommonResult<?> preview(PreviewPosterParam param) {
        return successAdmin( posterService.preview(param));
    }

}